package com.tws.pctask.controller;

import com.tws.common.handler.MyException;
import com.tws.common.model.ResponseWrapper;
import com.tws.pctask.model.registerManageModel.CheckEmailParam;
import com.tws.pctask.model.registerManageModel.CheckNameParam;
import com.tws.pctask.model.userManageModel.AddOrUpdateUserParam;
import com.tws.pctask.model.userManageModel.DelUserParam;
import com.tws.pctask.model.userManageModel.GetUserInfoParam;
import com.tws.pctask.model.userManageModel.GetUserListParam;
import com.tws.pctask.service.UserManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Description： 用户管理控制层
 * @Author: wbf
 * @Date: 2022/1/21 13:53
 */
@RestController
@RequestMapping("common/userManage")
@CrossOrigin
public class UserManageController {
    @Autowired
    private UserManageService userManageService;
    /**
     * 3.1获取用户身份列表
     * @return 身份列表
     */
    @GetMapping("getUserIdentityList")
    public ResponseWrapper getUserIdentityList(){
        return userManageService.getUserIdentityList();
    }

    /**
     * 3.2获取用户状态列表
     * @return 状态列表
     */
    @GetMapping("getUserStateList")
    public ResponseWrapper getUserStateList(){
        return userManageService.getUserStateList();
    }

    /**
     * 3.2获取用户列表
     * @param param 关键字查询和分页参数
     * @return 用户列表
     */
    @GetMapping("getUserList")
    public ResponseWrapper getUserList(@Validated GetUserListParam param){
        return userManageService.getUserList(param);
    }

    /**
     * 3.3检查用户名是否重复
     * @param param 用户名
     * @return true为重复 false为不重复
     */
    @GetMapping("checkName")
    public ResponseWrapper checkName(@Validated CheckNameParam param){
        return userManageService.checkName(param);
    }

    /**
     * 3.4检查邮箱是否重复
     * @param param 邮箱
     * @return true为重复 false为不重复
     */
    @GetMapping("checkEmail")
    public ResponseWrapper checkEmail(CheckEmailParam param){
        return userManageService.checkEmail(param);
    }

    /**
     * 3.5新增或修改用户
     * @param param 对应用户参数
     * @return 结果在数据库中查看
     */
    @PostMapping("addOrUpdateUser")
    public ResponseWrapper addOrUpdateUser(@Validated @RequestBody AddOrUpdateUserParam param){
        if(param.getUserEmail() == null || param.getUserEmail().equals("")) {
            throw new MyException("邮箱不能为空！");
        }
        return userManageService.addOrUpdateUser(param);
    }

    /**
     * 3.6获取用户的信息
     * @param param 用户id
     * @return 指定用户的信息
     */
    @GetMapping("getUserInfo")
    public ResponseWrapper getUserInfo(@Validated GetUserInfoParam param){
        return userManageService.getUserInfo(param);
    }

    /**
     * 3.7删除用户
     * @param param 用户id
     * @return 数据库查看结果
     */
    @PostMapping("delUser")
    public ResponseWrapper delUser(@RequestBody DelUserParam param){
        return userManageService.delUser(param);
    }
}
